/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans;

import java.sql.Timestamp;
import java.io.Serializable;
import Utilities.Constants;
import Utilities.Functions;
import Utilities.Interfaces.SelfValidating;

/**
 * This class is used for storing server data when writing it to or retrieving it
 * from the database. <br><br>
 *
 * <b>Note:</b> <i>Usernames and passwords are not stored when the object is
 *              built with information from the database.</i>
 *
 * @author Angel Sanadinov
 */
public class ServerBean implements Serializable, SelfValidating
{
    private int serverId;               //id of the server *
    private Timestamp dateAdded;        //timestamp of creation date *
    private String name;                //server name
    private String description;         //server description
    private String webServiceAddress;   //web service URL
    private String webServiceUser;      //web service username **
    private String webServicePassword;  //web service password **
    private String sharedFoldersRoot;   //root directory for shared folders
    private int vrdpPortsRangeLow;      //range of available VRDP ports: low limit
    private int vrdpPortsRangeHigh;     //range of available VRDP ports: high limit
    private int vrdpPortsPerMachine;    //number of ports per virtual machine
    private String vrdpAllocatedSlots;  //the slots currently allocated *
    //* only available when object is built with data from the database
    //** not available when object is built with data from the database

    /**
     * Constructs a new object, most often used when server data is to be put in the database.
     *
     * @param name the server name
     * @param description the server description
     * @param address the web service URL of the server
     * @param username the web service username
     * @param password the web service password
     * @param foldersRoot root directory for shared folders
     * @param vrdpPortsRangeLow lower boundary of the VRDP ports range for the server
     * @param vrdpPortsRangeHigh higher boundary of the VRDP ports range for the server
     * @param vrdpPortsPerMachine number of ports for each virtual machine
     */
    public ServerBean(String name, String description, String address, String username, 
                      String password, String foldersRoot, int vrdpPortsRangeLow,
                      int vrdpPortsRangeHigh, int vrdpPortsPerMachine)
    {
        this.name = name;
        this.description = description;
        this.webServiceAddress = address;
        this.webServiceUser  = username;
        this.webServicePassword = password;
        this.serverId = Constants.INVALID_SERVER_ID;
        this.dateAdded = null;
        this.sharedFoldersRoot = foldersRoot;
        this.vrdpPortsRangeLow = vrdpPortsRangeLow;
        this.vrdpPortsRangeHigh = vrdpPortsRangeHigh;
        this.vrdpPortsPerMachine = vrdpPortsPerMachine;
        this.vrdpAllocatedSlots = null;
    }

    /**
     * Constructs a new object, most often used when server data is retrieved from the database.
     *
     * @param serverId the id of the server
     * @param dateAdded the date on which the server was added to the database
     * @param name the server name
     * @param description the server description
     * @param address the web service URL of the server
     * @param username the web service username
     * @param password the web service password
     * @param foldersRoot root directory for shared folders
     * @param vrdpPortsRangeLow lower boundary of the VRDP ports range for the server
     * @param vrdpPortsRangeHigh higher boundary of the VRDP ports range for the server
     * @param vrdpPortsPerMachine number of ports for each virtual machine
     * @param vrdpAllocatedSlots list of ports currently allocated
     */
    public ServerBean(int serverId, Timestamp dateAdded, String name, String description, 
                      String address, String username, String password, String foldersRoot,
                      int vrdpPortsRangeLow, int vrdpPortsRangeHigh, int vrdpPortsPerMachine,
                      String vrdpAllocatedSlots)
    {
        this(name, description, address, username, password, foldersRoot,
             vrdpPortsRangeLow, vrdpPortsRangeHigh, vrdpPortsPerMachine);
        this.serverId = serverId;
        this.dateAdded = dateAdded;
        this.vrdpAllocatedSlots = vrdpAllocatedSlots;
    }

    /**
     * Checks the validity of the object. <br><br>
     *
     * This object is considered valid only when its constructed with data from the database.
     *
     * @return <code>true</code> if the object is valid or <code>false</code> otherwise
     */
    @Override
    public boolean isValid()
    {
        if(Functions.isServerIdValid(serverId) && Functions.isTimestampValid(dateAdded)
                && name != null && webServiceAddress != null && sharedFoldersRoot != null
                && vrdpPortsRangeLow > 0 && vrdpPortsRangeLow < vrdpPortsRangeHigh
                && vrdpPortsPerMachine > 0)
            return true;
        else
            return false;
    }

    /**
     * Retrieves the id of the server associated with this object, if available.
     *
     * @return the server id
     */
    public int getServerId()
    {
        return serverId;
    }

    /**
     * Retrieves a copy of the date on which the server was added to the database.
     * 
     * @return the date on which the server was added
     * @throws NullPointerException when this method is called on an object that was
     *                              not build with data from the database (no date is available).
     */
    public Timestamp getDateAdded() throws NullPointerException
    {
        return (Timestamp)dateAdded.clone();
    }

    /**
     * Retrieves the name of the server.
     *
     * @return the server name
     */
    public String getName() 
    {
        return name;
    }    

    /**
     * Retrieves the server description.
     *
     * @return the server description
     */
    public String getDescription()
    {
        return description;
    }

    /**
     * Retrieves the URL of web service associated with the server.
     * 
     * @return the web service URL
     */
    public String getWebServiceAddress()
    {
        return webServiceAddress;
    }

    /**
     * Retrieves the username for the web service, if available.
     *
     * @return the web service username
     * @see ServerBean#ServerBean(java.lang.String, java.lang.String,
     *      java.lang.String, java.lang.String, java.lang.String) Constructor
     */
    public String getWebServiceUser()
    {
        return webServiceUser;
    }

    /**
     * Retrieves the password for the web service, if available.
     *
     * @return the web service password
     * @see ServerBean#ServerBean(java.lang.String, java.lang.String,
     *      java.lang.String, java.lang.String, java.lang.String) Constructor
     */
    public String getWebServicePassword()
    {
        return webServicePassword;
    }

    /**
     * Retrieves the root directory to be used for shared folders.
     *
     * @return the shared folders root directory
     */
    public String getSharedFoldersRoot()
    {
        return sharedFoldersRoot;
    }

    /**
     * Retrieves the lower boundary of the VRDP ports range for the server.
     *
     * @return the lower range boundary
     */
    public int getVrdpPortsRangeLow()
    {
        return vrdpPortsRangeLow;
    }

    /**
     * Retrieves the higher boundary of the VRDP ports range for the server.
     *
     * @return the higher range boundary
     */
    public int getVrdpPortsRangeHigh()
    {
        return vrdpPortsRangeHigh;
    }

    /**
     * Retrieves the number of ports each virtual machine is allocated.
     *
     * @return the number of ports per machine (number of ports in a slot)
     */
    public int getVrdpPortsPerMachine()
    {
        return vrdpPortsPerMachine;
    }

    /**
     * Retrieves the list of slots that have already been allocated.
     *
     * @return the list of allocated slots
     */
    public String getVrdpAllocatedSlots()
    {
        return vrdpAllocatedSlots;
    }

    /**
     * Builds a CSV list of the data stored in the object. <br><br>
     *
     * Format: <i>serverId,dateAdded,name,description,webServiceAddress,sharedFoldersRoot,
     *            vrdpPortsRangeLow,vrdpPortsRangeHigh,vrdpPortsPerMachine,(vrdpAllocatedSlots)</i>
     *
     * @return the <code>String</code> representation of the object
     */
    @Override
    public String toString()
    {
        return serverId + "," + dateAdded + "," + name + "," + description 
               + "," + webServiceAddress + "," + sharedFoldersRoot 
               + "," + vrdpPortsRangeLow + "," + vrdpPortsRangeHigh
               + "," + vrdpPortsPerMachine + ",(" + vrdpAllocatedSlots + ")";
    }
}
